<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>后台管理系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f9f9f9;
        }

        h1 {
            text-align: center;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }

        table, th, td {
            border: 1px solid #ddd;
        }

        th, td {
            padding: 10px;
            text-align: left;
        }

        th {
            background-color: #f2f2f2;
        }

        .actions button {
            margin: 0 5px;
            padding: 5px 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .actions .edit {
            background-color: #4CAF50;
            color: white;
        }

        .actions .delete {
            background-color: #f44336;
            color: white;
        }

        .add-btn {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-bottom: 20px;
        }

        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            justify-content: center;
            align-items: center;
        }

        .modal-content {
            background: white;
            padding: 20px;
            border-radius: 8px;
            width: 400px;
            text-align: center;
        }

        .modal-content input {
            width: 90%;
            padding: 10px;
            margin: 10px 0;
        }

        .modal-content button {
            padding: 10px 20px;
            margin: 10px 5px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .save-btn {
            background-color: #4CAF50;
            color: white;
        }

        .cancel-btn {
            background-color: #f44336;
            color: white;
        }
    </style>
</head>

<body>
<h1>后台管理系统</h1>
<button class="add-btn" onclick="openModal('add')">添加数据</button>
<button class="add-btn" onclick="tuichu()">退出登录</button>
<table>
    <thead>
    <tr>
        <th>ID</th>
        <th>名称</th>
        <th>邮箱</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="data-table">
    <% data.forEach(function(row) { %>
        <tr>
            <td><%= row.id %></td>
            <td><%= row.name %></td>
            <td><%= row.email %></td>
            <td class="actions">
                <button class="edit" onclick="openModal('edit',<%= row.id %>, '<%= row.name %>', '<%= row.email %>')">编辑</button>
                <button class="delete" onclick="deleteData(<%= row.id %>)">删除</button>
            </td>
        </tr>
    <% }); %>

    </tbody>
</table>

<!-- 模态框 -->
<div class="modal" id="modal">
    <div class="modal-content">
        <h2 id="modal-title">添加数据</h2>
        <input type="hidden" id="modal-id">
        <input type="text" id="modal-name" placeholder="名称">
        <input type="email" id="modal-email" placeholder="邮箱">
        <button class="save-btn" onclick="saveData()">保存</button>
        <button class="cancel-btn" onclick="closeModal()">取消</button>
    </div>
</div>

<script>
    function tuichu(){
        window.location.href = '/';
    }

    function openModal(type, id = null, name = '', email = '') {

        const modal = document.getElementById('modal');
        document.getElementById('modal-title').textContent = type === 'add' ? '添加数据' : '编辑数据';
        document.getElementById('modal-id').value = id || ''; // 确保 id 正确传递
        document.getElementById('modal-name').value = name;   // 确保 name 正确传递
        document.getElementById('modal-email').value = email; // 确保 email 正确传递
        modal.style.display = 'flex';
    }


    function closeModal() {
        document.getElementById('modal').style.display = 'none';
    }

    function saveData() {
        const id = document.getElementById('modal-id').value;
        const name = document.getElementById('modal-name').value;
        const email = document.getElementById('modal-email').value;

        if (!name || !email) {
            alert('名称和邮箱不能为空！');
            return;
        }

        const data = {
            id: id,
            name: name,
            email: email
        };

        const url = id ? '/data/update' : '/data/add';

        fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'  // 设置请求头为 JSON
            },
            body: JSON.stringify(data)  // 将数据转为 JSON 字符串发送
        })
            .then(response => {
                if (response.ok) {
                    location.reload();  // 成功后刷新页面
                } else {
                    alert('操作失败！');
                }
            });
    }


    function deleteData(id) {
        const isConfirmed = confirm('确定要删除这条数据吗？');
        if (!isConfirmed) {
            return;
        }

        fetch('/data/delete', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: `id=${id}`
        }).then(response => {
            if (response.ok) {
                location.reload();
            } else {
                alert('删除失败！');
            }
        });
    }
</script>
</body>

</html>
